<?php
declare (strict_types = 1);

namespace lib;

class Utils {
	static int $seq			= 0;
	static int $unixtime	= 0;
    protected static function ToUrlParams($data){
		$buff = "";
		foreach ($data as $k => $v)
		{
			if($k == 'sign') continue;
			if(is_array($v) || is_object($v) || is_null($v)) continue;
				$buff .= $k . "=" . $v . "&";	
		}
		
		$buff = trim($buff, "&");
		return $buff;
	}

	public static function Sign($data, $api_key_conf){
        $api_key = \sys\Config::get($api_key_conf);
		ksort($data); //键值排序.
		return strtoupper(md5(static::ToUrlParams($data)."&key=".$api_key));
	}

	/**
	 * 生成订单号
	 */
	public static function genOrderNo(string $pfx) : string {
		if(static::$unixtime != ($unixtime = time())){
			static::$unixtime = $unixtime;
			static::$seq = 0;
		}
		return $pfx . 
			date('YmdHis') . 
			\app\index\BootStarup::$sid_36 . 
			str_pad(strtoupper(base_convert((string)++static::$seq, 10, 36)), 6, '0', STR_PAD_LEFT);
	}
}